package com.pkk.penguin.frame.know.boot.config.handle.zhiyi.db.support.util;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.LinkedHashMap;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @description: 执行工具类
 * @author: peikunkun
 * @create: 2020-02-28 16:11
 **/
@Component
public class ExecUtil {

  /**
   * 连接数据库信息
   */
  public static JSONObject PINGTAI_KEY_INFO;
  /**
   * 是否读取txt的执行sql
   */
  public static boolean READ_TXT_EXE_SQL = false;

  /**
   * 之易数据库配置文件
   */
  private static String ZHIYI_DB_CONFIG;

  //配置就覆盖默认的
  @Value("${com.pkk.config.zhiyi.db.execpath}")
  public void setZhiyiSynchConfig(String configPath) {
    ZHIYI_DB_CONFIG = configPath;
    if (org.apache.commons.lang.StringUtils.isBlank(ZHIYI_DB_CONFIG)) {
      throw new IllegalStateException("请指定之易数据库配置文件");
    }
    PINGTAI_KEY_INFO = readJsonFile(ZHIYI_DB_CONFIG);
  }


  //读取json文件
  public static JSONObject readJsonFile(String fileName) {
    String jsonStr = "";
    jsonStr = readFileToString(fileName);
    if (jsonStr == null) {
      return null;
    }
    if (StringUtils.isBlank(jsonStr)) {
      return null;
    }
    return new JSONObject(JSONArray.parseArray(jsonStr, LinkedHashMap.class).get(0));
  }


  /**
   * 读取配置文件
   *
   * @param fileName
   * @return
   */
  public static String readExeSQLFromTxt(String fileName) {
    READ_TXT_EXE_SQL = true;
    return readFileToString(fileName);
  }

  /**
   * @Description: 清除上次执行结果, 以防止下次继续执行
   * @Param: fileName
   * @Param time
   * @Param result
   * @return: void
   * @Author: peikunkun
   * @Date: 2020/3/3 0003 上午 10:16
   */
  public static String cleanExeSQL(String fileName, Long time, JSONArray result) {
    return exeSQLLog(fileName, time, result, true);
  }

  private static String exeSQLLog(String fileName, Long time, JSONArray result, boolean isClean) {
    final File file = new File(fileName);
    if (isClean) {
      FileUtil.deleteContents(file);
    }
    String content = ""
        + "\n┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n"
        + "┃\n"
        + "┃ 时间:" + DateUtil.now() + "\n"
        + "┃ 耗时:" + time + "ms\n"
        + "┃ 结果:" + result + "\n"
        + "┃\n"
        + "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n\n";
    cn.hutool.core.io.FileUtil.appendUtf8String(content, file);
    return content;
  }


  /**
   * @Description: 追加到上次执行结果后, 以防止下次继续执行
   * @Param: fileName
   * @Param time
   * @Param result
   * @return: void
   * @Author: peikunkun
   * @Date: 2020/3/3 0003 上午 10:16
   */
  public static String appentExeSQL(String fileName, Long time, JSONArray result) {
    return exeSQLLog(fileName, time, result, false);
  }


  /**
   * @Description: 读取配置文件转为String
   * @Param: fileName
   * @return: java.lang.String
   * @Author: peikunkun
   * @Date: 2020/3/2 0002 下午 1:17
   */
  public static String readFileToString(String fileName) {
    String jsonStr;
    try {
      File jsonFile = new File(fileName);
      FileReader fileReader = new FileReader(jsonFile);
      Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
      int ch = 0;
      StringBuffer sb = new StringBuffer();
      while ((ch = reader.read()) != -1) {
        sb.append((char) ch);
      }
      fileReader.close();
      reader.close();
      jsonStr = sb.toString();
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }
    return jsonStr;
  }
}
